/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Application;
import Application.Elements.*;
import java.util.Scanner;
import java.io.PrintStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 *
 * @author Stikmann
 */
public class TextBasedController extends NetworkFileParser{

    /**
     * Futtatja az egyes parancsokat amit kap az egyik streamen, majd kiirja az
     * esetleges eredmenyeket a masik streamre
     * @param sc
     * @param out
     */
    private void runCommands(Scanner sc,PrintStream out){
        net=new Network();//konnyebb mint mindenhol nullra tesztelni
        while(sc.hasNextLine())
        {
            String line=sc.nextLine().toLowerCase().trim();
            String cmd[]=line.split("[ \t]+");
            if (cmd.length==0 ||cmd[0].length()==0)
                continue;
            else if(cmd[0].equals("quit") || cmd[0].equals("exit"))
                break;
            else if(cmd[0].equals("load")){
                try{
                    parse(cmd[1]);
                    out.println("OK");
                }catch(Exception ex)
                {
                    out.println("HIBA: "+ex);
                }
                String hiba=net.check();
                if (hiba!=null)
                    out.println("HIBA: "+hiba);
            }
            else if(cmd[0].equals("reset"))
                net.reset();
            else if(cmd[0].equals("sim")){
                try{
                    net.simulate();
                    if (oscs!=null)
                        for (Osc elem:oscs)
                            elem.step();
                    if (btns!=null)
                        for (BTN elem:btns)
                            out.println(elem.name+": "+(elem.getOut()?"1":"0"));
                    
                    if (wavegens!=null)
                        for (Wavegen elem:wavegens){
                            out.print(elem.name+": ");
                            for (boolean jel: elem.sequence)
                                out.print(jel?"1":"0");
                            out.println(" "+elem.index+" "+(elem.getOut()?"1":"0"));
                        }

                    if (leds!=null)
                        for (LED elem:leds)
                            out.println(elem.name+": "+(elem.getOut()?"1":"0"));
                        
                    if (oscs!=null)
                        for (Osc elem:oscs){
                            out.print(elem.name+": ");
                            for (boolean jel: elem.history)
                                out.print(jel?"1":"0");
                            out.println();
                        }
                }
                catch(UnstableException ex){
                    out.println("HIBA: "+ex);
                }
            }
            else if(cmd[0].equals("change")){
                if (cmd.length<3)
                    out.println("HIBA: A CHANGE  parancs legalabb ket parametert var.");
                boolean voltilyen=false;
                if (btns!=null)
                    for (BTN btn: btns)
                        if (btn.name.equals(cmd[1]))
                        {
                            btn.setErtek(cmd[2].equals("1"));
                            voltilyen=true;
                        }
                if (!voltilyen)
                    out.println("HIBA: Nincs ilyen nevu gomb");
            }
            else if(cmd[0].equals("step")){
                if (wavegens!=null)
                    for (Wavegen elem:wavegens)
                        elem.step();
            }
            else
                out.println("HIBA: Ismeretlen parancs \""+cmd[0]+"\"");
        }
    }

    /**
     * Interaktiv futtatas (konzolos input)
     */
    public void run(){
        runCommands(new Scanner(System.in),System.out);
    }

    /**
     * Futtatas fajlbol kapott inputtal es fajlba rakott outputtal
     * @param infile Bemeneti file neve. Nem art, ha letezik ;)
     * @param outfile Kimeneti file neve. Nem art, ha irhato
     */
    public void run(String infile,String outfile){
        try{
            Scanner  fil1=new Scanner(new FileInputStream (infile));
            PrintStream fil2=new PrintStream(new FileOutputStream(outfile));
            runCommands(fil1,fil2);
            fil1.close();
            fil2.close();
        }catch(java.io.IOException ex){
            System.out.println("HIBA a TextBasedController.run() meghivasanal: "+ex);
        }
    }
}
